package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberProfileService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import javax.websocket.server.PathParam;

/**
 * 会员板块
 */
@RestController
@RequestMapping("/member")
@CrossOrigin
public class MemberProfileController {
    @Reference
    private MemberProfileService memberProfileService;

    //@PathParam("currentPage") Integer currentPage,
    //@PathParam("pageSize") Integer pageSize,
    //@PathParam("total") Integer total,
    //@PathParam("queryString") String queryString

    /**
     * 分页查询会员信息
     * @param currentPage
     * @param pageSize
     * @param total
     * @param queryString
     * @return
     */
    @GetMapping("/findPage")
    public Result pageQuery(@PathParam("currentPage") Integer currentPage,
                            @PathParam("pageSize") Integer pageSize,
                            @PathParam("total") Integer total,
                            @PathParam("queryString") String queryString
    ) {
        try {
            PageResult pageResult = (PageResult) memberProfileService.pageQuery(currentPage, pageSize, total, queryString);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    /**
     * 添加会员信息
     * @param member
     * @return
     */
    @PostMapping("/add")
    public Result addMember(@RequestBody Member member) {
        int i = memberProfileService.addMember(member);
        if (i == 0) {
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL, true);
        }
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    /**
     * 修改会员信息
     * @param member
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Member member ) {
        int result = memberProfileService.updateById(member);
        if (result != 0) {
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS,true);
        }
        return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        try {
            //查询检查项
            Member member = memberProfileService.selectById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    //根据id禁用会员
    @PutMapping("/{id}/{status}")
    public Result updateStatus(@PathVariable Integer id,
                               @PathVariable Integer status){
        int result = memberProfileService.updateStatus(id,status);
        if (result != 0) {
            return new Result(true, MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS,true);
        }
        return new Result(false, MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
    }
}
